package com.wsm.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsm.common.annotation.RedisCache;
import com.wsm.common.domain.BlackipDO;
import com.wsm.common.enums.BaseEnum;
import com.wsm.common.mapper.BlackipMapper;
import com.wsm.common.service.BlackipService;
import com.wsm.common.service.RedisService;
import com.wsm.common.utils.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.wsm.common.enums.Constant.LIMIT_KEY;

/**
 * 访问黑名单
 *
 * @author wangshimin
 * @email wangshiminhome@foxmail.com
 * @date 2020-02-17 17:03:32
 * @description : 访问黑名单
 */
@Service
public class BlackipServiceImpl extends ServiceImpl<BlackipMapper, BlackipDO> implements BlackipService {
	@Autowired
	private BlackipMapper blackipMapper;
	@Autowired
	RedisService redisService;

	@Override
	@RedisCache(value = "Blackip", flush = false, expire = 30L, unit = TimeUnit.MINUTES)
	public List<BlackipDO> list(Map<String, Object> map) {
		return blackipMapper.list(map);
	}

	@Override
	@RedisCache(value = "Blackip", flush = false, expire = 30L, unit = TimeUnit.MINUTES)
	public int count(Map<String, Object> map) {
		return blackipMapper.count(map);
	}

	@Override
	@RedisCache(value = "Blackip", flush = true, expire = 30L, unit = TimeUnit.MINUTES)
	public int orSave(BlackipDO blackip) {
		blackip.setId(SnowFlake.nextId());
		String limitKey = LIMIT_KEY + blackip.getIp();
		if (!redisService.hasKey(limitKey)) {
			redisService.set(limitKey, 0);
		}
		return blackipMapper.orSave(blackip);
	}

	@Override
	@RedisCache(value = "Blackip", flush = true, expire = 30L, unit = TimeUnit.MINUTES)
	public int update(BlackipDO blackip) {
		return blackipMapper.orUpdate(blackip);
	}

	@Override
	@RedisCache(value = "Blackip", flush = true, expire = 30L, unit = TimeUnit.MINUTES)
	public int remove(Long id) {
		BlackipDO blackipDO = blackipMapper.selectById(id);
		redisService.del(LIMIT_KEY + blackipDO.getIp());
		return blackipMapper.remove(id);
	}

	@Override
	@RedisCache(value = "Blackip", flush = true, expire = 30L, unit = TimeUnit.MINUTES)
	public int batchRemove(Long[] ids) {
		return blackipMapper.batchRemove(ids);
	}

	@Override
	@RedisCache(value = "Blackip", flush = true, expire = 30L, unit = TimeUnit.MINUTES)
	public int removeOrDel(Long id) {
		BlackipDO blackipDO = blackipMapper.selectById(id);
		redisService.del(LIMIT_KEY + blackipDO.getIp());
		return blackipMapper.removeOrDel(id);
	}

	@Override
	@RedisCache(value = "Blackip", flush = true, expire = 30L, unit = TimeUnit.MINUTES)
	public int batchRemoveOrDel(Long[] ids) {
		return blackipMapper.batchRemoveOrDel(ids);
	}

	@Override
	@RedisCache(value = "Blackip", flush = true, expire = 30L, unit = TimeUnit.MINUTES)
	public int updateUseFlag(Long id, Integer useFlag) {
		Map<String, Object> updateUseFlagMap = new HashMap<>(2);
		updateUseFlagMap.put("id" , id);
		updateUseFlagMap.put("useFlag" , useFlag);
		if (useFlag == BaseEnum.USE_FLAG.YES.getCode()) {
			BlackipDO blackipDO = blackipMapper.selectById(id);
			redisService.set(LIMIT_KEY + blackipDO.getIp(), 0);
		} else {
			BlackipDO blackipDO = blackipMapper.selectById(id);
			redisService.del(LIMIT_KEY + blackipDO.getIp());
		}
		return blackipMapper.updateUseFlag(updateUseFlagMap);
	}
}
